<template>
    <el-card>
        <div slot="header">
            <span>管理员信息</span>
        </div>
        <div>
            管理员ID:{{ id }}
            <el-divider></el-divider>
            账号:{{ account }}
            <el-divider></el-divider>
            用户组:{{ userGroup }}
            <el-divider></el-divider>
            创建时间:{{ ctime | formater }}
            <el-divider></el-divider>
            修改头像:
            <el-upload class="avatar-uploader" action="http://sell.h5.itsource.cn:8087/users/avatar_upload"
                :show-file-list="false" :on-success="Success" :before-upload="beforeUpload">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </div>

    </el-card>
</template>

<script>
import local from '@/utils/local.js'
import { avatarEdit } from '@/api/user.js'
export default {
    data () {
        let userinfo = local.get('userinfo')
        return {
            imageUrl: '',
            ...userinfo
        };
    },

    methods: {
        //上传成功后执行的回调函数
        async Success (res, file) {
            if (res.code === 0) {
                this.$message.success(res.msg);
                this.imageUrl = res.imgUrl;
            }
            //将上传的图片保存到数据库
            await avatarEdit({ imgUrl: res.imgUrl });

            //传参数到顶部组件
            this.$bus.$emit('sendImg', res.imgUrl)

        },
        //上传之前执行的函数
        beforeUpload (file) {
            //MIME：文件类型的描述
            let mime = ['image/jpeg', 'image/png', 'image/gif']
            const isJPG = mime.includes(file.type);
            const isLt2M = file.size / 1024 / 1024 < 2;
            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG,PNG,GIF 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        }
    }
}
</script>
<style>
.avatar-uploader {
    display: inline-block;
    vertical-align: middle;
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
}

.avatar {
    width: 100px;
    height: 100px;
    display: block;
}
</style>